package com.whosly.fast.web.quickly.command;

import com.whosly.fast.web.quickly.config.INormalConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Env Print
 */
@Component
public class EnvPrintCommandLineRunner implements CommandLineRunner, InitializingBean, ApplicationContextAware {
    private static final Logger logger = LoggerFactory.getLogger(EnvPrintCommandLineRunner.class);

    /**
     * INormalConfiguration 接口的所有实现类
     */
    private Map<String, INormalConfig> configServiceMap;

    @Override
    public void run(String... args) throws Exception {
        if (configServiceMap != null && !configServiceMap.isEmpty()) {
            for (Map.Entry<String, INormalConfig> entry: configServiceMap.entrySet()) {
                logger.info("{}: {}. \n", entry.getKey(), entry.getValue().toString());
            }
        }

        logger.info("EnvPrintCommandLineRunner Finish.");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //.
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 通过上下文，根据接口类型返回相应的所有实现类bean
        configServiceMap = applicationContext.getBeansOfType(INormalConfig.class);
    }
}
